/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.sql.*;
/**
 *
 * @author EuiHyun
 */
public class BranchDAO extends AbstractBranchDAO {

    @Override
    public BranchDTO find(String branchName) throws PersistanceException {
        BranchDTO dto = null;
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select BRANCHNAME, CHANGEUNITID, ORIGINCHANGEUNITID from APP.BRANCH where BRANCHNAME = '"+branchName+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                dto = new BranchDTO();
                
                dto.setBranchName(rs.getString("BRANCHNAME"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setOriginChangeUnitId(rs.getString("ORIGINCHANGEUNITID"));
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing BranchDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dto; 
    }

    @Override
    public List<BranchDTO> findByChangeUnitId(String changeUnitId) throws PersistanceException {
        List<BranchDTO> dtoArray = new ArrayList<BranchDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select BRANCHNAME, CHANGEUNITID, ORIGINCHANGEUNITID from APP.BRANCH where CHANGEUNITID = '"+changeUnitId+"'";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                BranchDTO dto = new BranchDTO();
                
                dto.setBranchName(rs.getString("BRANCHNAME"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setOriginChangeUnitId(rs.getString("ORIGINCHANGEUNITID"));
                
                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing BranchDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }    
    
    @Override
    public List<BranchDTO> list() throws PersistanceException {
        
        List<BranchDTO> dtoArray = new ArrayList<BranchDTO>();
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select BRANCHNAME, CHANGEUNITID, ORIGINCHANGEUNITID from APP.BRANCH";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                BranchDTO dto = new BranchDTO();
                dto.setBranchName(rs.getString("BRANCHNAME"));
                dto.setChangeUnitId(rs.getString("CHANGEUNITID"));
                dto.setOriginChangeUnitId(rs.getString("ORIGINCHANGEUNITID"));

                dtoArray.add(dto);
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing BranchDAO.list: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dtoArray; 
    }

    @Override
    public void insert(BranchDTO branchDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
//        String sql="insert into APP.BRANCH (BRANCHNAME, CHANGEUNITID, ORIGINCHANGEUNITID ) " 
//                    + "values ('"+branchDTO.getBranchName()+"','"+branchDTO.getChangeUnitId()+"','"+branchDTO.getOriginChangeUnitId()+"')";
  
        String sql="insert into APP.BRANCH (BRANCHNAME, CHANGEUNITID, ORIGINCHANGEUNITID ) " 
                    + "values (?,?,?)";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true); 
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1,branchDTO.getBranchName());
            stmt.setString(2,branchDTO.getChangeUnitId());
            stmt.setString(3,branchDTO.getOriginChangeUnitId());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing BranchDAO.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }

    @Override
    public void update(BranchDTO branchDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        String sql="update APP.BRANCH set BRANCHNAME=?"
                + " ,CHANGEUNITID=?"
                + " ,ORIGINCHANGEUNITID=?"
                + " where BRANCHNAME=?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, branchDTO.getBranchName());
            stmt.setString(2, branchDTO.getChangeUnitId());
            stmt.setString(3, branchDTO.getOriginChangeUnitId());
            stmt.setString(4, branchDTO.getBranchName());
            
            stmt.executeUpdate(); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing BranchDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }     
    }

    @Override
    public void delete(String branchName) throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.BRANCH "  
                   +"WHERE BRANCHNAME = '"+branchName+"'";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing BranchDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }

}
